<?php
/**
 * 对权限进行管理  后台操作
 * 功能:增加新的权限
 *@author wolf  [Email: 116311316@qq.com]
 *@since 2011-07-20
 *@version 1.0
 */
abstract class NodeController extends Action {
	// 登录用户id
	protected $_user_global = array ('uid' => 0, 'username' => null, 'groupid' => 0 );
	// 系统默认管理员 权限从上到下 默认是5
	protected $_group = array (1 => "超级管理员", 2 => "管理员", 3 => "验证用户", 4 => "登录用户", 5 => "游客", 6 => "黑名单", 0 => "继承分类" );
	
	/**
	 * 检验权限
	 */
	public function _routeDown() {
		$controller = $this->getControllerName (); // 当前控制器
		$action = $this->getActionName (); // 当前动作
		$islogin = false;
		//如果cookie存在 那么自动生成session
		if (isset ( $_COOKIE ['user'] )) {
			$userInfo = MemberModel::instance ()->getOneMember ( array ('username' => $_COOKIE ['user'] ) );
			$groupInfo = MemberModel::instance ()->getOneGroup ( array ('id' => $userInfo ['groupid'] ) );
			$this->_user_global = array ('uid' => $userInfo ['uid'], 'manager' => $userInfo ['manager'], 'username' => $userInfo ['username'], 'face' => $userInfo ['face'], 'realname' => $userInfo ['real_name'], 'verify' => $userInfo ['verify'], 'groupname' => $groupInfo ['groupname'], 'groupid' => $userInfo ['gruopid'] );
			$islogin = true;
			$this->view ()->assign ( 'user', $this->_user_global ); // 导入用户信息
		}
		
		// 排除无需权限方法  
		$allowAction = require 'authorize.php';
		
		//模块访问权限 
		

		if (! in_array ( $action, ($allowAction [$controller]) )) {
			
			if ($userInfo ['manager'] < 1 || $islogin == false) {
				$error = "需要管理员权限才能进入";
				$this->authorizeTemp ( $error );
				exit ();
			}
		
		}
		
		// 游客验证 登录之后 有注册会员 实名认证 管理员 和超级管理员之分 非管理管理员 都需要操作权限检验
		

		/* 检查被访问的页面 是否需要权限 */
		if ($action == 'c' || $action == 'i') {
			$category = NewsModel::instance ()->getCateogryById ( $_REQUEST ['cid'] );
			
			$this->validteNode ( $category ['groupid'] );
		}
		
		if ($action == 'v') {
			// 内容权限大于分类权限 如果为7 就是继承分类权限 分类默认是游客
			$news = NewsModel::instance ()->getNewsWhere ( array ('id' => $_REQUEST ['id'] ) );
			if ($news ['groupid'] == 0) {
				$category = NewsModel::instance ()->getCateogryById ( $news ['cid'] );
				$news ['groupid'] = $category ['groupid'];
			}
			
			$this->validteNode ( $news ['groupid'] );
		}
		
		$nodes = $this->getNodes ();
		$this->view ()->assign ( 'nodes', $nodes );
		//获取菜单 操作方法
		$this->view ()->assign ( 'action', $this->_actionName );
		$this->view ()->assign ( 'module', $this->_controllerName );
		
		//载入回复数
		$noreads = NewsModel::instance ()->getCommentNoRead ();
		$this->view ()->assign ( 'noreads', $noreads ['num'] );
	}
	
	private function getNodes() {
		$nodes = NodeModel::instance ()->getNodeAll ( array ('fid' => 0 ) );
		
		foreach ( $nodes as $k => $v ) {
			$rs = null;
			$rs = NodeModel::instance ()->getNodeAll ( array ('fid' => $v ['id'] ) );
			$nodes [$k] ['son'] = $rs;
		}
		return $nodes;
	
	}
	/**
	 *
	 * @param String $type
	 * LOGIN ADMIN VERIFY SUPER
	 */
	public function validteNode($type) {
		if ($type == 5) {
			$this->isLogin ();
		}
		if ($type == 3) {
			$this->isVerify ();
		}
		if ($type == 2) {
			$this->isAdmin ();
		}
		if ($type == 1) {
			$this->isSuperAdmin ();
		}
	}
	// 是否登录
	public function isLogin() {
		if ($this->_user_global ['groupid'] > 4) {
			$error = '你尚未登录';
			$this->authorizeTemp ( $error );
		}
		return true;
	}
	
	// 是否实名认证
	public function isVerify() {
		if ($this->_user_global ['verify'] < 1) {
			$error = '你的权限不够，未实名认证';
			
			$this->authorizeTemp ( $error );
		}
		return true;
	}
	// 是否是管理员
	public function isAdmin() {
		if ($this->_user_global ['manager'] < 0) {
			$error = '你的权限不够，需要管理员权限';
			
			$this->authorizeTemp ( $error );
		}
		return true;
	}
	// 是否是站长
	public function isSuperAdmin() {
		if ($this->_user_global ['manager'] == 1 || empty ( $this->_user_global ['groupid'] )) {
			$error = '你的权限不够，需要站长权限';
			$this->authorizeTemp ( $error );
		}
		return true;
	}
	public function authorizeTemp($error) {
		$this->view ()->assign ( 'error', $error );
		$this->view ()->clearCache ( 'mysql:authorize.tpl' );
		$this->view ()->clearCompiledTemplate ( 'mysql:authorize.tpl' );
		$this->view ()->display ( 'mysql:authorize.tpl' );
		exit ();
	}
	
	/**
	 * 获取权限 id ,fid ,name
	 *
	 * @param int $selectId
	 * 选中的
	 * @param boolen $onchang
	 * 开启js跳转
	 * @param int $fid
	 * 父类
	 * @param string $selectName
	 * 选择name 和id值
	 */
	protected function getNode($selectId = NULL, $onchang = false, $fid = NULL, $selectName = "category") {
		$Tree = new Category ();
		// setNode(目录ID,上级ID，目录名字);
		$categorys = NodeModel::instance ()->getCategory ( $fid );
		foreach ( $categorys as $c ) {
			$Tree->setNode ( $c ['id'], $c ['fid'], $c ['remark'] );
		}
		$category = $Tree->getChilds ();
		// 遍历输出
		foreach ( $category as $key => $id ) {
			if ($selectId == $id) {
				$options .= "<option value=$id selected>" . $Tree->getLayer ( $id, '|-' ) . $Tree->getValue ( $id ) . "</option>";
			} else {
				$options .= "<option value=$id>" . $Tree->getLayer ( $id, '|-' ) . $Tree->getValue ( $id ) . "</option>";
			}
		}
		if ($onchang) {
			$cate = "onchange=\"javascript:location.href='/member/editMemberGroup/?mid='+(this.value)\">" . $options . "</select>";
		} else {
			$cate = $options . "</select>";
		}
		$cate = "<select name=$selectName id=$selectName ><option value=0>添加应用程序</option>" . $cate;
		return $cate;
	}
	
	/**
	 * 添加分类和属性配置
	 */
	public function edit() {
		$node = $this->getNode ();
		$this->view ()->assign ( 'node', $node );
		if ($_POST ['add'] && $_POST ['node'] != '' && $_POST ['remark'] != '') {
			// 只要应用程序不重复就没关系
			$checkRs = NodeModel::instance ()->checkCateNum ( array ('node' => $_POST ['node'] ) );
			if (! empty ( $checkRs )) {
				$this->redirect ( '该名称已有！', '/node/edit', 2 );
				exit ();
			}
			NodeModel::instance ()->addCategory ( array ('node' => $_POST ['node'], 'fid' => $_POST ['category'], 'remark' => $_POST ['remark'] ) );
			$this->redirect ( '添加成功!', '/node/edit', 2 );
		} else if ($_POST ['del']) {
			$cateNum = NodeModel::instance ()->checkCateNum ( array ('fid' => $_POST ['category'] ) );
			if ($cateNum > 0) {
				$this->redirect ( '此分类下还有子类，不能删除!', '/node/edit', 2 );
				exit ();
			} else {
				NodeModel::instance ()->deleteCategory ( array ('id' => $_POST ['category'] ) );
				$this->redirect ( '删除成功!', '/node/edit', 2 );
			}
		} else if ($_POST ['rename']) {
			if ($_POST ['node'] != '' && $_POST ['remark'] != '') {
				$arrV = array ('node' => $_POST ['node'], 'remark' => $_POST ['remark'] );
			} else if ($_POST ['node'] != '' && $_POST ['remark'] == '') {
				$arrV = array ('node' => $_POST ['node'] );
			} else if ($_POST ['node'] == '' && $_POST ['remark'] != '') {
				$arrV = array ('remark' => $_POST ['remark'] );
			} else {
				$this->redirect ( '你没有更新任何内容!', '/node/edit', 2 );
				exit ();
			}
			NodeModel::instance ()->renameCategory ( $arrV, array ('id' => $_POST ['category'] ) );
			$this->redirect ( '更新成功!', '/node/edit', 2 );
		} else {
			$this->view ()->clearCache ( 'file:node/edit.tpl' );
			$this->view ()->display ( 'file:node/edit.tpl' );
		}
	}

}